import 'dart:math';

import 'package:fl_chart/fl_chart.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ty_smart_watch/page/health/controller/common_controller.dart';
import 'package:ty_smart_watch/utils/Log.dart';
import 'package:ty_smart_watch/utils/pair.dart';

class ChartView extends StatelessWidget {
  final Color color;
  final double maxY;

  const ChartView({super.key, required this.color, required this.maxY});

  @override
  Widget build(BuildContext context) {
    final CommonController controller = Get.find();

    return Container(
        margin: const EdgeInsets.only(right: 20),
        height: 160,
        child: Obx(
          () => BarChart(
            BarChartData(
              maxY: maxY,
              alignment: BarChartAlignment.spaceEvenly,
              gridData: const FlGridData(show: false),
              borderData: FlBorderData(
                show: true,
                border: const Border(
                  bottom: BorderSide(color: Colors.grey), // 底部边线的样式
                  left: BorderSide(color: Colors.grey), // 左侧边线的样式
                  right: BorderSide(color: Colors.transparent), // 右侧边线的样式
                  top: BorderSide(color: Colors.transparent), // 顶部边线的样式，这里设置为透明来隐藏
                ),
              ),
              barGroups:
                  _getBarData(controller.dateTypeIndex.value == 0, controller.dataList.value),
              titlesData: FlTitlesData(
                show: true,
                topTitles: const AxisTitles(
                  sideTitles: SideTitles(showTitles: false),
                ),
                rightTitles: const AxisTitles(
                  sideTitles: SideTitles(showTitles: false),
                ),
                bottomTitles: AxisTitles(
                  sideTitles: SideTitles(
                    showTitles: true,
                    reservedSize: 30,
                    getTitlesWidget: getTitles,
                  ),
                ),
                // bottomTitles: AxisTitles(
                //   sideTitles: _bottomTitles,
                // ),
              ),
            ),
          ),
        ));
  }

  Widget getTitles(double value, TitleMeta meta) {
    final CommonController controller = Get.find();
    const style = TextStyle(
      // fontWeight: FontWeight.bold,
      fontSize: 12,
    );
    String text;
    int length = controller.dataList.value.length;
    if (length == 7) {
      switch (value.toInt()) {
        case 0:
          text = '日';
          break;
        case 1:
          text = '一';
          break;
        case 2:
          text = '二';
          break;
        case 3:
          text = '三';
          break;
        case 4:
          text = '四';
          break;
        case 5:
          text = '五';
          break;
        case 6:
          text = '六';
          break;
        default:
          text = '';
          break;
      }
    } else if (length == 24) {
      switch (value.toInt()) {
        case 0:
          text = '00:00';
          break;
        case 4:
          text = '04:00';
          break;
        case 8:
          text = '08:00';
          break;
        case 12:
          text = '12:00';
          break;
        case 16:
          text = '16:00';
          break;
        case 20:
          text = '20:00';
          break;
        default:
          text = '';
          break;
      }
    } else {
      switch (value.toInt()) {
        case 0:
        case 5:
        case 10:
        case 15:
        case 20:
        case 25:
          text = '${value.toInt() + 1}';
        default:
          if (value.toInt() == length - 1) {
            text = '${value.toInt() + 1}';
          } else {
            text = '';
          }
          break;
      }
    }

    return SideTitleWidget(
      axisSide: meta.axisSide,
      space: 4,
      child: Text(text, style: style),
    );
  }

  List<BarChartGroupData> _getBarData(bool isDay, List<double> list) {
    Log.d("isDay: $isDay");
    if (isDay) {
      return _getDayData(list);
    } else {
      return _getWeekMonthData(list);
    }
  }

  List<BarChartGroupData> _getWeekMonthData(List<double> list) {
    List<BarChartGroupData> resultList = [];
    List<Color> colors = [Color(0xFF2229B6), Color(0xFF2F82FA), Color(0xFF46BDFD)];
    for (int i = 0; i < list.length; i++) {
      var total = Random().nextInt(20);
      var res = BarChartGroupData(
        x: i,
        barRods: [
          BarChartRodData(
              toY: total * 2,
              rodStackItems: [
                BarChartRodStackItem(0, total / 3, colors[0]),
                BarChartRodStackItem(total / 3, total / 3 * 2, colors[1]),
                BarChartRodStackItem(total / 3 * 2, total.toDouble(), colors[2]),
              ],
              color: Colors.transparent,
              borderRadius: BorderRadius.zero),
          // backDrawRodData: BackgroundBarChartRodData(color:Colors.grey, show: true, toY: 200),
          //     BarChartRodData(
          //         toY: list[i] * 2,
          //         color: Colors.yellow,
          //         backDrawRodData: BackgroundBarChartRodData(color: Colors.purple, show: true, toY: 200),
          //         borderRadius: BorderRadius.zero),
        ],
      );
      resultList.add(res);
    }
    return resultList;
  }

  List<BarChartGroupData> _getDayData(List<double> list) {
    List<BarChartGroupData> resultList = [];
    var tempCount = 0;
    for (int i = 0; i < list.length; i++) {
      tempCount++;
      var total = Random().nextInt(20);
      var res = BarChartGroupData(
        x: i,
        barRods: [
          BarChartRodData(
              toY: total * 2,
              rodStackItems: [
                BarChartRodStackItem(0, total / 3, _func(tempCount % 4)[0]),
                BarChartRodStackItem(total / 3, total / 3 * 2, _func(tempCount % 4)[1]),
                BarChartRodStackItem(total / 3 * 2, total.toDouble(), _func(tempCount % 4)[2]),
              ],
              color: Colors.transparent,
              // backDrawRodData: BackgroundBarChartRodData(color: Colors.red, show: false, toY: 200),
              borderRadius: BorderRadius.zero),
        ],
      );
      resultList.add(res);
    }
    return resultList;
  }

  List<Color> _func(int index) {
    if (index == 0) {
      return [Color(0xFF2229B6), Colors.white, Colors.white];
    } else if (index == 1) {
      return [Colors.white, Color(0xFF2F82FA), Colors.white];
    } else if (index == 3) {
      return [Colors.white, Color(0xFF2F82FA), Colors.white];
    } else {
      return [Colors.white, Colors.white, Color(0xFFFFA6C3)];
    }
  }
}
